﻿#region Using directives
using System;
using System.Data;
using System.Data.Common;
using System.Data.SqlClient;
using System.Web.UI.WebControls;
using InvoiceChecking.Data;
using InvoiceChecking.Entities;
using InvoiceChecking.Web.UI;
using WebCommon;

#endregion

public partial class JournalPage : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        FormUtil.RedirectAfterUpdate(GridView1, "JournalVoucher.aspx?page={0}");
        FormUtil.SetPageIndex(GridView1, "page");
        FormUtil.SetDefaultButton((Button)GridViewSearchPanel1.FindControl("cmdSearch"));
    }

    protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
    {
        string urlParams = string.Format("JournalVoucherId={0}", GridView1.SelectedDataKey.Values[0]);
        Response.Redirect("JournalVoucherEdit.aspx?" + urlParams, true);
    }

    protected void btnApply_Click(object sender, EventArgs e)
    {
        if (ddlAction.SelectedValue == "Print")
        {
            var returnNum = CreatePrintList();
            if (returnNum > 0)
            {
                Response.Redirect("ReportJournalVoucher.aspx", true);
            }
        }
        else if (ddlAction.SelectedValue == "Close")
        {
            var journalVouchers = GetSelectedRow();
            journalVouchers = journalVouchers.FindAll("IsCompleted", false);
            foreach (var journalVoucher in journalVouchers)
            {
                journalVoucher.IsCompleted = true;
                DataRepository.JournalVoucherProvider.Update(journalVoucher);
            }
            GridView1.DataBind();
        }
    }


    private TList<JournalVoucher> GetSelectedRow()
    {
        var journalVouchers = new TList<JournalVoucher>();
        foreach (GridViewRow row in GridView1.Rows)
        {
            var cbCheck = (CheckBox)row.FindControl("cb_Check");
            if (cbCheck.Checked)
            {
                var journalVoucherId = GridView1.DataKeys[row.DataItemIndex].Value.ToString();
                var journalVoucher = DataRepository.JournalVoucherProvider.GetByJournalVoucherId(journalVoucherId);
                journalVouchers.Add(journalVoucher);
            }
        }
        return journalVouchers;
    }

    private int CreatePrintList()
    {
        DataTable journalVouchers = null;
        DataTable journalDetails = null;
        int returnNum = 0;
        foreach (GridViewRow row in GridView1.Rows)
        {
            var cbCheck = (CheckBox)row.FindControl("cb_Check");
            if (cbCheck.Checked)
            {
                var journalVoucherId = GridView1.DataKeys[row.DataItemIndex].Value.ToString();
                DbCommand cmd = new SqlCommand();
                cmd.CommandText = "_JournalVoucher_Print";
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add(new SqlParameter("JournalVoucherId", journalVoucherId));
                var journalResult = DataRepository.Provider.ExecuteDataSet(cmd);
                if (journalVouchers == null)
                    journalVouchers = journalResult.Tables[0].Copy();
                else
                    foreach (DataRow dataRow in journalResult.Tables[0].Rows)
                        journalVouchers.Rows.Add(dataRow.ItemArray);
                if (journalDetails == null)
                    journalDetails = journalResult.Tables[1].Copy();
                else
                    foreach (DataRow dataRow in journalResult.Tables[1].Rows)
                        journalDetails.Rows.Add(dataRow.ItemArray);

                returnNum++;
            }
        }
        Session["Journal"] = journalVouchers;
        Session["JournalDetail"] = journalDetails;
        return returnNum;
    }

    protected string GetStatus(string journalVoucherId)
    {
        var journalStatus = GetJournalStatus(journalVoucherId);
        return SharedFunction.AddSpacesToSentence(journalStatus.ToString(), false);
    }

    protected string GetClass(string journalVoucherId)
    {
        var journalStatus = GetJournalStatus(journalVoucherId);
        return journalStatus.ToString();
    }
    public enum JournalStatus
    {
        Approved,
        Rejected,
        NotSend,
        Waiting,
        Void
    }

    public JournalStatus GetJournalStatus(string journalVoucherId)
    {
        var journalVoucher = DataRepository.JournalVoucherProvider.GetByJournalVoucherId(journalVoucherId);
        if (journalVoucher.IsVoid)
            return JournalStatus.Void;
        if (journalVoucher.IsApproved)
            return JournalStatus.Approved;
        if (journalVoucher.NotifyCode == "Rejected")
            return JournalStatus.Rejected;
        if (journalVoucher.IsSentForApproval == false)
            return JournalStatus.NotSend;
        return JournalStatus.Waiting;
    }
}


